<?php

class AccountSettingsForm extends Form
{
	public $NewPasswordMailModule = 'System/Data/Auth';
	public $NewPasswordMailTemplate = 'templates/newpassword.tpl';
	public $NewPasswordMailSubject = 'Your New Password';
	
	public $NewEmailMailModule = 'System/Data/Auth';
	public $NewEmailMailTemplate = 'templates/newemail.tpl';
	public $NewEmailMailSubject = 'Confirm Your New Email Address';
	
	public function AccountSettingsForm()
	{
		parent::__construct();
		$this->NewPasswordMailSubject = translate('Your New Password');
		$this->NewEmailMailSubject = translate('Confirm Your New Email Address');
		$this->Name = 'account';
		$this->Title = translate('Account Settings');
		$this->ButtonText = translate('Save Changes');		
	}
	
	public function FormAction()
	{
		if(session('user')!='')
		{
			$bean = UserManager::GetUser(session('user'));
			$this->Bean = $bean;
			
			// add controls to section
			//$this->Controls[] = a2o( 'FormControl', array( 'Bean' => $bean, 'FieldName' => 'login', 'Title' => translate('Loginname') ) );
			$this->Controls[] = a2o( 'FormControl', array( 'Bean' => $bean, 'FieldName' => 'fullname', 'Title' => translate('Full name') ) );
			$emailControl = a2o( 'FormControl', array( 'FieldName' => 'email', 'Title' => translate('Email'), 'IsBound' => false ) );
			$emailControl->SetValue($bean->email);
			$this->Controls[] = $emailControl;
			$passwordControl = a2o( 'FormControl', array( 'FieldName' => 'password', 'Title' => translate('New Password'), 'IsBound' => false, 'Type' => 'password', 'Validator' => new StringLengthValidator(8, true) ) );
			$this->Controls[] = $passwordControl;
			$passwordReControl = a2o( 'FormControl', array( 'FieldName' => 'password_re', 'Title' => translate('New Password(re)'), 'IsBound' => false, 'Type' => 'password' ) );
			$this->Controls[] = $passwordReControl;
			$passwordOldControl = a2o( 'FormControl', array( 'FieldName' => 'password_old', 'Title' => translate('Password'), 'IsBound' => false, 'Type' => 'password' ) );
			$this->Controls[] = $passwordOldControl;
		
			if( $this->LoadPostData())
			{
				$password = $passwordControl->GetValue( );
				$passwordRe = $passwordReControl->GetValue( );
				$passwordOld = $passwordOldControl->GetValue( );
				$email = $emailControl->GetValue( );
				
				// if password correct
				if(UserManager::Authenticate($_SESSION['user'], $passwordOld))
				{
					// change email
					if($email != $bean->email)
					{
						$bean->email = $email;
						$bean->isemailconfirmed = false;
						
						// send confirmation email
						$ConfirmURL = ConfirmRequestManager::GenerateRequestURL('http://localhost/confirmemail', $bean->login, $email );
						$to = $bean->fullname . '<' . $email . '>';					
						$mailTemplate = a2o('TemplateLoader', array( 'Module' => $this->NewEmailMailModule, 'Template' => $this->NewEmailMailTemplate,
							'User' => $bean->login, 'FullName' => $bean->fullname, 'ConfirmURL' => $ConfirmURL ) );
						Mail::Send($to, $this->NewEmailMailSubject, $mailTemplate->GetText());
						//mail($to, $this->NewEmailMailSubject, $mailTemplate->translate( ));
						//$this->Messages[] = $mailTemplate->translate();
						
						$this->Messages[] = translate('Your email address has changed. You will receive an email to confirm your new email address.');
					}
					
					// changepassword
					if($password != '' || $passwordRe != '')
					{
						if($password == $passwordRe)
						{
							// change password
							$bean->passwordsalt = PasswordHasher::GenerateSalt( );
							$bean->passwordhash = PasswordHasher::GenerateHash( $password, $bean->passwordsalt );

							// send email with password
							$to = $bean->fullname . '<' . $bean->email . '>';					
							$mailTemplate = a2o('TemplateLoader', array( 'Module' => $this->NewPasswordMailModule, 'Template' => $this->NewPasswordMailTemplate,
								'User' => $bean->login, 'Password' => $password, 'FullName' => $bean->fullname ) );
							Mail::Send($to, $this->NewPasswordMailSubject, $mailTemplate->GetText());
							//mail($to, $this->NewPasswordMailSubject, $mailTemplate->translate( ));
							//$this->Messages[] = $mailTemplate->translate();
							
							$this->Messages[] = translate('Your password has changed. You will receive a notification email.');
						}
						else
						{
							$this->Messages[] = translate('The new passwords do not match. Password is not changed.');
						}
					}

					R::store($bean);
				}
				else
					$this->Messages[] = translate('Your password is incorrect.');
			}
			else
				if($passwordControl->GetValue() == '')
					$passwordReControl->SetValue('');
		}
		else
		{
			$this->Messages[] = translate('You are not logged in.');
			$this->HideControls = true;
		}
	}
}
